﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TaiLieu;

public partial class login : System.Web.UI.UserControl
{
    public int tam = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Sessions.maXacThuc < 10000 || Sessions.maXacThuc > 99999)
            Sessions.maXacThuc = tam = sinhma();
        if (tam == 0)
            tam = Sessions.maXacThuc;

    }
    protected void WriteCookie()
    {
        if (chbRemember.Checked == true)
        {
            HttpCookie cookie = new HttpCookie("remember", "yes");
            Response.Cookies.Add(cookie);
            cookie = new HttpCookie("idmembergiaoducanbinh");
            cookie.Values.Add("UserName", txtUserName.Text.Trim());
            cookie.Values.Add("Password", txtpass.Text.Trim());
            Response.Cookies.Add(cookie);
        }
    }
    
    int sinhma()
    {
        Random random = new Random();
        return random.Next(10000, 99999);
    }
    protected void cmdDangNhap_Click(object sender, EventArgs e)
    {
        if (Sessions.maXacThuc != ConvertType.ToInt(this.txtMaAnToan.Text))
        {
            Sessions.maXacThuc = tam = sinhma();
            this.errorMess.InnerHtml = "<script>alert('Mã xác thực sai. Mời bạn nhập lại!');</script>";
        }
        else
        {
            Sessions.members = MembersDB.login(this.txtUserName.Text.Trim(), this.txtpass.Text);
            if (Sessions.members == null)
            {
                Sessions.maXacThuc = tam = sinhma();
                this.txtMaAnToan.Text = "";
                this.errorMess.InnerHtml = "<script>alert('Kiểm tra lại tên đăng nhập và mật khẩu !');</script>";
            }
            else
            {
                WriteCookie();
                Sessions.maXacThuc = 0;
                Response.Redirect("default.aspx?/trang-chu");
            }
        }

    }


}